#include "util/logger/logger.h"
#include <log4cpp/PatternLayout.hh>
#include <log4cpp/OstreamAppender.hh>
#include <log4cpp/PropertyConfigurator.hh>
#include <log4cpp/Priority.hh>
#include <iostream>
#include <string>

namespace log4cpp_helper {
// const char kLoggerConfPath = "./conf/log4cpp.properties";  // 配置文件路径

Logger *Logger::instance_ = new Logger();

Logger::Logger() : rootCategory_(log4cpp::Category::getRoot()),
                    infoCategory_(log4cpp::Category::getInstance(std::string("infoCategory"))),
                    errorCategory_(log4cpp::Category::getInstance(std::string("warnCategory"))) {
}

Logger::~Logger() {
    rootCategory_.info("shutdown logger");
    delete Logger::instance_;
}

void Logger::init(const std::string &cfg_path) {
    log4cpp::PropertyConfigurator::configure(cfg_path);
    infoCategory_.info("init logger successfully");
}

void Logger::log(log4cpp::Priority::PriorityLevel priority, const char *stringFormat, ...) {
    va_list args;
    va_start(args, stringFormat);

    switch (priority) {
        case (log4cpp::Priority::PriorityLevel::DEBUG):
        case (log4cpp::Priority::PriorityLevel::INFO):
            infoCategory_.logva(priority, stringFormat, args);
            break;
        case (log4cpp::Priority::PriorityLevel::WARN):
        case (log4cpp::Priority::PriorityLevel::ERROR):
            errorCategory_.logva(priority, stringFormat, args);
            break;
        default:
            errorCategory_.error("priority [%d] is not supported now");
    }

    va_end(args);
}


}  // namespace log4cpp_helper